<template>
  <div class="role-list">
    <el-card class="box-card">
      <template
        #header
        class="clearfix"
      >
        <!-- <span>筛选搜索</span> -->
        <el-form
          ref="searchDom"
          :model="form"
        >
          <el-form-item
            label="角色名称"
            prop="name"
          >
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :disabled="loading"
              @click="onSubmit"
            >
              查询搜索
            </el-button>
            <el-button
              :disabled="loading"
              @click="onReset"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </template>
      <el-button @click="handleAdd">
        添加角色
      </el-button>
      <el-table
        v-loading="loading"
        :data="roles"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="编号"
        />
        <el-table-column
          prop="name"
          label="角色名称"
        />
        <el-table-column
          prop="description"
          label="描述"
        />
        <el-table-column
          prop="createdTime"
          label="添加时间"
        />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
        >
          <template #default="scope">
            <div>
              <el-button
                type="text"
                @click="$router.push({
                  name: 'alloc-menu',
                  params: {
                    roleId: scope.row.id
                  }
                })"
              >
                分配菜单
              </el-button>
              <el-button
                type="text"
                @click="$router.push({
                  name: 'alloc-resource',
                  params: {
                    roleId: scope.row.id
                  }
                })"
              >
                分配资源
              </el-button>
            </div>
            <div>
              <el-button
                type="text"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑角色' : '添加角色'"
      width="50%"
    >
      <create-or-edit
        v-if="dialogVisible"
        :role-id="roleId"
        :is-edit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, onBeforeMount, Ref, ref } from 'vue'
import { getRoles, deleteRole } from '/@/services/role'
import CreateOrEdit from './CreateOrEdit.vue'
import { ElMessageBox, ElMessage } from 'element-plus'

interface Role {
  code: string
  createdBy: number | string
  createdTime: Date
  description: string
  id: number
  name: string
  operatorId: number | null
  updatedBy: number | string
  updatedTime: Date
}

const form: Ref = ref({})
const useInitRolesLoad = () => {
  const loading = ref(false)
  const roles = ref<Role[]>([])
  form.value = ref({
    current: 1,
    size: 50,
    name: ''
  })
  const rolesLoad = async () => {
    loading.value = true
    const { data } = await getRoles(form.value)
    roles.value = data.data.records as []
    loading.value = false
  }
  // 进入页面加载数据
  onBeforeMount(rolesLoad)
  return {
    loading,
    roles,
    form,
    rolesLoad
  }
}
const useHandles = (loadDataCallback: ()=>void) => {
  const searchDom = ref()
  const onSubmit = () => {
    loadDataCallback()
  }
  const handleDelete = async (role: Role) => {
    try {
      await ElMessageBox.confirm(`确认删除角色：${role.name}？`, '删除提示')
      await deleteRole(role.id)
      ElMessage.success('删除成功')
      loadDataCallback()
    } catch (err) {
      if (err && err.response) {
        ElMessage.error('删除失败，请重试')
      } else {
        ElMessage.info('取消删除')
      }
    }
  }
  const onReset = () => {
    form.value.name = ''
    searchDom.value.resetFields()
    console.log('onReset')
  }
  return {
    searchDom,
    onReset,
    handleDelete,
    onSubmit
  }
}
const useDialog = (loadDataCallback: ()=>void) => {
  const dialogVisible = ref(false)
  const isEdit = ref(false)
  const roleId = ref<number|null>(null)
  const handleEdit = (role: Role) => {
    dialogVisible.value = true
    isEdit.value = true
    roleId.value = role.id
  }
  const handleAdd = () => {
    isEdit.value = false
    dialogVisible.value = true
  }
  const onSuccess = () => {
    dialogVisible.value = false // 关闭对话框
    loadDataCallback() // 重新加载数据列表
  }
  return {
    isEdit,
    roleId,
    dialogVisible,
    handleAdd,
    handleEdit,
    onSuccess
  }
}
export default defineComponent({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  setup () {
    const initCombine = useInitRolesLoad()
    const { rolesLoad } = initCombine
    return {
      ...initCombine,
      ...useHandles(rolesLoad),
      ...useDialog(rolesLoad)
    }
  }
})
// export default defineComponent({
//   name: 'RoleList',
//   components: {
//     CreateOrEdit
//   },
//   data () {
//     return {
//       roles: [], // 角色列表
//       form: {
//         current: 1,
//         size: 50,
//         name: ''
//       }, // 查询条件
//       loading: false,
//       dialogVisible: false, // 控制添加/编辑角色的对话框显示和隐藏
//       roleId: null, // 编辑角色的 ID
//       isEdit: false
//     }
//   },

//   created () {
//     this.loadRoles()
//   },

//   methods: {
//     async loadRoles () {
//       // this.loading = true
//       // const { data } = await getRoles(this.form)
//       // this.roles = data.data.records
//       // this.loading = false
//     },

//     onSubmit () {
//       this.loadRoles()
//     },

//     handleEdit (role: any) {
//       this.dialogVisible = true
//       this.roleId = role.id
//       this.isEdit = true
//     },

//     async handleDelete (role: any) {
//       // try {
//       //   await this.$confirm(`确认删除角色：${role.name}？`, '删除提示')
//       //   await deleteRole(role.id)
//       //   this.$message.success('删除成功')
//       //   this.loadRoles()
//       // } catch (err) {
//       //   if (err && err.response) {
//       //     this.$message.error('删除失败，请重试')
//       //   } else {
//       //     this.$message.info('取消删除')
//       //   }
//       // }
//     },

//     onReset () {
//       // (this.$refs.form as Form).resetFields()
//       // this.loadRoles()
//     },

//     onSuccess () {
//       this.dialogVisible = false // 关闭对话框
//       this.loadRoles() // 重新加载数据列表
//     },

//     handleAdd () {
//       this.isEdit = false
//       this.dialogVisible = true
//     }
//   }
// })
</script>

<style lang="scss" scoped></style>
